package cn.com.youyuan.SDK.core;

import java.util.Vector;

import cn.com.youyuan.SDK.interfaces.DataStorageAgent;

public class PersistentThread extends Thread
{
	private final CoreModule m_coreModule;
	private final Vector m_dataPool;
	private DataStorageAgent m_dsa;

	public PersistentThread(CoreModule cm)
	{
		m_coreModule = cm;
		m_dataPool = new Vector(2);
		start();
	}

	public void run()
	{
		try
		{
			DataAction da = null;
			while (true)
			{
				try
				{
					synchronized (m_dataPool)
					{
						if (m_dataPool.size() == 0)
						{
							m_dataPool.wait();
							continue;
						}
						else
						{
							da = (DataAction) m_dataPool.elementAt(0);
							m_dataPool.removeElementAt(0);
						}
					}
					executeNotify(da);
				}
				catch (InterruptedException ie)
				{
					log(getClass(), "The module thread is interrupted");
				}
				catch (Exception e)
				{
					log(getClass(), e);
				}
			}
		}
		catch (Exception e)
		{
			log(getClass(), "Root exception on THREAD#3");
			log(getClass(), e);
		}
		catch (Error e)
		{
			log(getClass(), "Root error on THREAD#3");
			log(getClass(), e.toString());
			e.printStackTrace();
		}
	}

	private void log(Class c, Exception e)
	{
		m_coreModule.log(c, e);
	}

	private void log(Class c, String s)
	{
		m_coreModule.log(c, s);
	}

	private void executeNotify(DataAction da)
	{
		synchronized (m_dsa)
		{
			if (da.mVersion != null)
			{
				m_dsa.setVersion(da.mType, da.mVersion);
			}
			else
			{

				if (da.mIsMultiId)
				{
//					log(getClass(), "put - type = " + da.mType + ", id = " + da.mIds + "(" + da.mIds.length + ")");
					m_dsa.put(da.mType, da.mIds, da.mBdes);
				}
				else
				{
//					log(getClass(), "put - type = " + da.mType + ", id = " + da.mId);
					m_dsa.put(da.mType, da.mId, da.mBde);
				}
			}
		}
	}

	protected void put(int dataType, String id, byte[] data)
	{
		synchronized (m_dataPool)
		{
			DataAction da = new DataAction();
			da.mType = dataType;
			da.mIsMultiId = false;
			da.mId = id;
			da.mBde = data;
			m_dataPool.addElement(da);
			m_dataPool.notify();
		}

	}

	protected void put(int dataType, String[] ids, byte[][] datas)
	{
		synchronized (m_dataPool)
		{
			DataAction da = new DataAction();
			da.mType = dataType;
			da.mIsMultiId = true;
			da.mIds = ids;
			da.mBdes = datas;
			m_dataPool.addElement(da);
			m_dataPool.notify();
		}
	}

	protected void setVersion(int dataType, String version)
	{
		synchronized (m_dataPool)
		{
			DataAction da = new DataAction();
			da.mType = dataType;
			da.mVersion = version;
			m_dataPool.addElement(da);
			m_dataPool.notify();
		}
	}

	public void clearPool()
	{
		synchronized (m_dataPool)
		{
			m_dataPool.removeAllElements();
		}
	}
	
	protected void setDataStorageAgent(DataStorageAgent dsa)
	{
		m_dsa = dsa;
	}

	class DataAction
	{
		String mVersion = null;

		int mType;
		boolean mIsMultiId;

		String mId;
		String[] mIds;

		byte[] mBde;
		byte[][] mBdes;
	}
}
